package com.starblink.wishlist.createboard.ui

import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.basic.apollo.ext.optional
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.library.widget.R
import com.starblink.library.widget.wishlist.bean.BatchProductArrEntry
import com.starblink.rocketreserver.CreateWishListMutation
import com.starblink.rocketreserver.MoveProductList2WishListMutation
import com.starblink.rocketreserver.type.CollectItem
import com.starblink.rocketreserver.type.WishListAddRequest
import kotlinx.coroutines.launch

class CreateBoardVM : BaseViewModel() {

    //        var mList: MutableLiveData<ArrayList<Any>> = MutableLiveData()
    inline fun createBoard(
        isWishListPage: Boolean,
        batchPros: ArrayList<BatchProductArrEntry>? = null,
        title: String,
        desc: String?,
        fromWishListId: String?,
        crossinline successCallback: () -> Unit
    ) {
        viewModelScope.launch {
            val wishResult = SkGqlClient.skMutation(
                CreateWishListMutation(
                    wishListAddRequest = WishListAddRequest(
                        title = title,
                        description = Optional.present(desc)
                    )
                ),
                showDefaultLoading = true
                //fetchPolicy = FetchPolicy.NetworkFirst
            )?.data?.createWishList
            wishResult?.let { wishlistId ->
                if (batchPros.isNullOrEmpty()) {
                    successCallback.invoke()
                } else {
                    val data = SkGqlClient.skMutation(
                        MoveProductList2WishListMutation(
                            items = arrayListOf<CollectItem>().apply {
                                batchPros!!.forEach {
                                    add(
                                        CollectItem(
                                            id = it.id,
                                            type = it.type.optional(),
                                        )
                                    )
                                }
                            }.optional(),
                            fromWishListId = fromWishListId?.optional() ?: Optional.Absent,
                            toWishListId = wishlistId.optional(),
                        ),
                        showDefaultLoading = true,
                    )
                    val success = data?.data?.moveProductList2WishList == true
                    if (success) {
                        FlowBus.boolFlowBusNotify(FlowConst.WISHLIST_BOARD_REFRESH)
                        if (!isWishListPage) {
                            toast(string(R.string.success_added_to_board).format(title))
                        } else {
                            FlowBus.with<String>(FlowConst.WISH_ALL_ITEMS_SHOW_TOAST_2)
                                .post(title)
                        }
                    } else {
                        data?.errors?.getOrNull(0)?.message?.let {
                            toast(it)
                        }
                    }
                    //只针对心愿单详情
                    FlowBus.with<ArrayList<BatchProductArrEntry>>(FlowConst.WISHLIST_DETAILS_MOVE_SUCCESS)
                        .post(batchPros)
                    successCallback.invoke()
                }
            }
        }
    }
}